﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.FrameworkSample (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends!       
//_______________________________________________________________________________________________________________________
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Sapiens.at.SharePoint.Receiver;
using Sapiens.at.FrameworkSample.Entities;
using Sapiens.at.SharePoint.Repositories;

namespace Sapiens.at.FrameworkSample.Receiver.EmployeeReceiver
{
    public class EmployeeReceiver : BaseItemReceiver
    {
        public EmployeeReceiver()
        {
            AddItemEventConfig(new ItemEventConfig
            {
                EventTypes = new SPEventReceiverType[] { SPEventReceiverType.ItemAdded, SPEventReceiverType.ItemUpdating },
                OnChangedFields = new string[] { Employee.LOOKUP_DEPARTMENT },
                Callback = OnDepartmentChange
            });

            AddItemEventConfig(new ItemEventConfig
            {
                EventTypes = new SPEventReceiverType[] { SPEventReceiverType.ItemDeleting},
                Callback = OnDeleting
            });
        }

        private void OnDepartmentChange(ItemEventArgs args)
        {
            var employee = (Employee)args.Entity;

            // Decrease the number of employees of the previous department
            if (args.ChangedValues.ContainsKey(Employee.LOOKUP_DEPARTMENT) &&
                !string.IsNullOrEmpty(Convert.ToString(args.ChangedValues[Employee.LOOKUP_DEPARTMENT])))
            {
                var repository = new ListRepository(Department.ListUrl, args.Web);
                var oldDepartment = repository.GetEntity<Department>(new SPFieldLookupValue(Convert.ToString(args.ChangedValues[Employee.LOOKUP_DEPARTMENT])).LookupId);
                oldDepartment.DecreaseEmployeeCount();
            }

            // Increase the nu,ber of employees of the new department
            employee.Department.IncreaseEmployeeCount();
        }

        private void OnDeleting(ItemEventArgs args)
        {
            var employee = (Employee)args.Entity;

            if (employee.Department != null)
            { // decrease the number of employees for its department
                employee.Department.DecreaseEmployeeCount();
            }
        }
    }
}
